package net.maku.association.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import net.maku.association.convert.InterestAssociationMemberConvert;
import net.maku.association.dao.InterestAssociationDao;
import net.maku.association.dao.InterestAssociationMemberDao;
import net.maku.association.entity.InterestAssociationMemberEntity;
import net.maku.association.query.InterestAssociationMemberQuery;
import net.maku.association.service.InterestAssociationMemberService;
import net.maku.association.vo.InterestAssociationMemberVO;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.needy.vo.NeedyMemberVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 兴趣协会成员
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-04-26
 */
@Service
@AllArgsConstructor
public class InterestAssociationMemberServiceImpl extends BaseServiceImpl<InterestAssociationMemberDao, InterestAssociationMemberEntity> implements InterestAssociationMemberService {

    private final InterestAssociationDao interestAssociationDao;

    private final InterestAssociationMemberDao interestAssociationMemberDao;
    @Override
    public PageResult<InterestAssociationMemberVO> page(InterestAssociationMemberQuery query) {
//        IPage<InterestAssociationMemberEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));
//        List<InterestAssociationMemberVO> list = InterestAssociationMemberConvert.INSTANCE.convertList(page.getRecords());
//        Long associationId = query.getAssociationId();
//        if(associationId != null){
//            InterestAssociationEntity interestAssociationEntity = interestAssociationDao.selectById(associationId);
//            list.forEach(l->{
//                l.setName(interestAssociationEntity.getName());
//            });
//        }

        IPage<InterestAssociationMemberEntity> pageParams = getPage(query);
        QueryWrapper<InterestAssociationMemberEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("association_id",query.getAssociationId());
        queryWrapper.orderByDesc("m.id");
        IPage<InterestAssociationMemberVO> page = interestAssociationMemberDao.selectPageList(pageParams,queryWrapper);
        return new PageResult<>(page.getRecords(), page.getTotal());
    }

    private LambdaQueryWrapper<InterestAssociationMemberEntity> getWrapper(InterestAssociationMemberQuery query){
        LambdaQueryWrapper<InterestAssociationMemberEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StringUtils.isNotBlank(query.getRealName()),InterestAssociationMemberEntity::getRealName,query.getRealName())
                .eq(StringUtils.isNotBlank(query.getOrgName()),InterestAssociationMemberEntity::getOrgName,query.getOrgName())
                .eq(query.getAssociationId() != null,InterestAssociationMemberEntity::getAssociationId,query.getAssociationId());
        return wrapper;
    }

    @Override
    public void save(InterestAssociationMemberVO vo) {
        InterestAssociationMemberEntity entity = InterestAssociationMemberConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(InterestAssociationMemberVO vo) {
        InterestAssociationMemberEntity entity = InterestAssociationMemberConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}